home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmiSoft / Util / conv / MIDI2asm.lha / MIDI2asm / MIDI2asm.rexx < prev   
OS/2 REXX Batch file  |  2004-01-14  |  4KB  |  171 lines

  1. /* $VER: MIDI2asm 1.1 $
  2.  * Convert MIDI data to 68k assembler source
  3.  * © by Stefan Haubenthal 1998/99/2003/04 */
  4. if ~open(midi,arg(1)) then exit 0*writeln(stdout,"Usage: MIDI2asm midifile")
  5. do i=0 to 11
  6.     melo.i=word("c db d eb e f gb g ab a bb b",i+1)
  7.     bass.i=word("C Db D Eb E F Gb G Ab A Bb B",i+1)
  8. end
  9. meta.00="seqnum"
  10. meta.01="text"
  11. meta.02="copyright"
  12. meta.03="sname"
  13. meta.04="iname"
  14. meta.05="lyric"
  15. meta.06="marker"
  16. meta.07="cue"
  17. meta.20="cprefix"
  18. meta.21="pprefix"
  19. meta.2F="trackend"
  20. meta.51="tempo"
  21. meta.54="smpte"
  22. meta.58="time"
  23. meta.59="key"
  24. meta.74="seqspec"
  25. ID.40="Kawai"
  26. ID.41="Roland"
  27. ID.42="Korg"
  28. ID.43="Yamaha"
  29. ID.44="Casio"
  30. ID.46="Kamiya"
  31. ID.47="Akai"
  32. ID.48="JapanVictor"
  33. ID.49="Mesosha"
  34.  
  35. say ";" arg(1) "V"word(sourceline(1),4)
  36. say
  37. say "SIZE    macro"
  38. say "    dc.b    \1>>24&$ff"
  39. say "    dc.b    \1>>16&$ff"
  40. say "    dc.b    \1>>8&$ff"
  41. say "    dc.b    \1&$ff"
  42. say "    endm"
  43. say "DELTA    macro"
  44. say "    ifgt    \1-16383"
  45. say "    dc.b    \1>>14|128"
  46. say "    endc"
  47. say "    ifgt    \1-127"
  48. say "    dc.b    \1>>7&$ff|128"
  49. say "    endc"
  50. say "    dc.b    \1&127"
  51. say "    endm"
  52. say "noteoff    =$80"
  53. say "noteon    =$90"
  54. say "polypress    =$A0"
  55. say "ctrl    =$B0"
  56. say "prog    =$C0"
  57. say "chanpress    =$D0"
  58. say "pitchbend    =$E0"
  59. say "sysex    =$F0"
  60. say "meta    =$FF"
  61. say "seqnum    =$00"    /* sequence_number  */
  62. say "text    =$01"    /* text_event  */
  63. say "copyright    =$02"    /* copyright_notice  */
  64. say "sname    =$03"    /* sequence_name     */
  65. say "iname    =$04"    /* instrument_name  */
  66. say "lyric    =$05"    /* lyric         */
  67. say "marker    =$06"    /* marker  */
  68. say "cue    =$07"    /* cue_point  */
  69. say "cprefix    =$20"    /* channel_prefix */
  70. say "pprefix    =$21"    /* port_prefix */
  71. say "trackend    =$2F"    /* end_of_track */
  72. say "tempo    =$51"    /* set_tempo  */
  73. say "smpte    =$54"    /* smpte_offset */
  74. say "time    =$58"    /* time_signature */
  75. say "key    =$59"    /* key_signature */
  76. say "seqspec    =$74"    /* sequencer_specific */
  77. do i=12 to 107
  78.     say centre(note(i)"=",4)i
  79. end
  80. say
  81. say "    data"
  82. say '    dc.b    "'readch(midi,4)'"'
  83. say "    dc.l    "c2d(readch(midi,4))
  84. say "    dc.w    "c2d(readch(midi,2)) "format"
  85. tracks=c2d(readch(midi,2))
  86. say "    dc.w    "tracks "tracks"
  87. tempo=c2d(readch(midi,2))*4
  88. say "    dc.w    "tempo/4 "tempo"
  89.  
  90. do track=1 to tracks
  91. say
  92. say "; track" track
  93. say '    dc.b    "'readch(midi,4)'"'
  94. len=c2d(readch(midi,4))
  95. /*say "    dc.l    "len "size"*/
  96. parse value d2x(len,8) with len1 3 len2 5 len3 7 len4
  97. say "    SIZE    $"len1||len2||len3||len4
  98. time=0
  99. measure=0
  100. do forever
  101.     data=c2d(readch(midi))
  102.     if data<128 then delta=data
  103.     else do
  104.         delta=(data-128)*128
  105.         data=c2d(readch(midi))
  106.         if data<128 then delta=delta+data
  107.         else delta=delta*128+(data-128)*128+c2d(readch(midi))
  108.     end
  109.     time=time+delta
  110.     if time/tempo>=measure then do
  111.         say "; measure" trunc(time/tempo)+1
  112.         measure=measure+1
  113.     end
  114.     say "    DELTA    "delta
  115.     data=c2x(readch(midi))
  116.     select
  117.         when data<"80" then /* running status */
  118.         say "    dc.b    "x2d(data)","c2d(readch(midi))
  119.         when left(data,1)="8" then
  120.         say "    dc.b    noteoff+"x2d(right(data,1))","note(c2d(readch(midi)))","c2d(readch(midi))
  121.         when left(data,1)="9" then
  122.         say "    dc.b    noteon+"x2d(right(data,1))","note(c2d(readch(midi)))","c2d(readch(midi))
  123.         when left(data,1)="A" then
  124.         say "    dc.b    polypress+"x2d(right(data,1))","c2d(readch(midi))","c2d(readch(midi))
  125.         when left(data,1)="B" then
  126.         say "    dc.b    ctrl+"x2d(right(data,1))","c2d(readch(midi))","c2d(readch(midi))
  127.         when left(data,1)="C" then
  128.         say "    dc.b    prog+"x2d(right(data,1))","c2d(readch(midi))
  129.         when left(data,1)="D" then
  130.         say "    dc.b    chanpress+"x2d(right(data,1))","c2d(readch(midi))
  131.         when left(data,1)="E" then
  132.         say "    dc.b    pitchbend+"x2d(right(data,1))","c2d(readch(midi))","c2d(readch(midi))
  133.         when data="F0" then do /* system exclusive */
  134.             len=c2d(readch(midi))
  135.             say "    dc.b    sysex,"len
  136.             call dump
  137.             end
  138.         when data="FF" then do /* meta data */
  139.             data=c2x(readch(midi))
  140.             len=c2d(readch(midi))
  141.             say "    dc.b    meta,"meta.data","len
  142.                 select
  143.                     when left(data,1)="0" then
  144.                         if len>1 then say '    dc.b    "'readch(midi,len)'"'
  145.                              else call dump
  146.                     when data="2F" then leave
  147.                     otherwise call dump
  148.                 end
  149.             end
  150.     end
  151. end
  152. end
  153. exit
  154.  
  155. dump:
  156. d=""
  157. do i=1 to len
  158.     d=d"$"c2x(readch(midi))
  159.     if i//8=0 | i=len then do
  160.         say "    dc.b    "d
  161.         d=""
  162.     end
  163.     else d=d","
  164. end
  165. return
  166.  
  167. note:
  168. o=arg(1)%12
  169. if o>=4 then return value("melo."arg(1)//12)o-4
  170.         else return value("bass."arg(1)//12)3-o
  171.